package top.andnux.weather.controller;

import android.content.Context;
import android.content.SharedPreferences;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

import top.andnux.weather.model.PhotoModel;
import top.andnux.weather.view.PhotoView;

public class PhotoController extends BaseController<PhotoModel, PhotoView> {

    public PhotoController(Context context) {
        super(context);
    }

    /**
     * 加载照片
     */
    public void loadImages() {
        if (isAttachView()) {
            view.showLoading("加载中...");
        }
        SharedPreferences preferences = context.getSharedPreferences("config", Context.MODE_PRIVATE);
        Set<String> paths = preferences.getStringSet("paths", new HashSet<>());
        if (isAttachView()) {
            view.showPhoto(new ArrayList<>(paths));
        }
        if (isAttachView()) {
            view.hideLoading();
        }
    }

    /**
     * 添加照片
     *
     * @param file
     */
    public void addImage(File file) {
        if (isAttachView()) {
            view.showLoading("加载中...");
        }
        SharedPreferences preferences = context.getSharedPreferences("config", Context.MODE_PRIVATE);
        Set<String> paths = preferences.getStringSet("paths", new HashSet<>());
        paths = new HashSet<>(paths);
        paths.add(file.getAbsolutePath());
        preferences.edit().putStringSet("paths", paths).commit();
        if (isAttachView()) {
            view.showPhoto(new ArrayList<>(paths));
        }
        if (isAttachView()) {
            view.hideLoading();
        }
    }
}
